package saucaca.fragment;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import lemon.kenhnhacvn.BaseActivity;
import lemon.kenhnhacvn.MainActivity;
import lemon.kenhnhacvn.R;
import saucaca.Adapter.SongAdapter;
import saucaca.Object.SongObject;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;


public class LocalSonglistExpanded extends Fragment {
	private MainActivity mActivity = null;
	SongAdapter adapter = null;
	ListView listview;
	String title;
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	}

	@SuppressLint("NewApi")
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
//		mActivity.getActionBar().hide();
		adapter = new SongAdapter(getActivity());
		View view = inflater.inflate(R.layout.song_list, container, false);
		findViewById(view);
		initData();
		mActivity.myActionBar.setMenu(title, false, false);
		return view;
	}
	@Override
	public void onHiddenChanged(boolean hidden) {
		super.onHiddenChanged(hidden);
		mActivity.myActionBar.setMenu(title, false, false);
	}

	public void findViewById(View view) {
		listview = (ListView) view.findViewById(R.id.layoutlistview_listview);
	}

	@SuppressWarnings("unchecked")
	private void initData() {
		Bundle b = getArguments();
		String id = b.getString(MainActivity.ID);
		int indext = b.getInt(MainActivity.INDEX);
		ArrayList<Map<String, Object>> newlist = new ArrayList<Map<String, Object>>();
		ArrayList<SongObject> musiclist = new ArrayList<SongObject>();
		if (id.equals(LocalAllMusic.ARTIST_MUSIC)) {
			musiclist = (ArrayList<SongObject>) BaseActivity.singerMusicMap.get(indext).get(BaseActivity.Key_List);
			title=getResources().getString(R.string.menu_casi) + "\n" + (String) BaseActivity.singerMusicMap.get(indext).get(BaseActivity.Key_Title);
		}
		if (id.equals(BaseActivity.ALBUM_MUSIC)) {
			musiclist = (ArrayList<SongObject>) BaseActivity.albumMusicMap.get(indext).get(BaseActivity.Key_List);
			title=getResources().getString(R.string.menu_album) + "\n" + (String) BaseActivity.albumMusicMap.get(indext).get(BaseActivity.Key_Title);
		}
		if (id.equals(BaseActivity.FILE_MUSIC)) {
			musiclist = (ArrayList<SongObject>) BaseActivity.fileMusicMap.get(indext).get(BaseActivity.Key_List);
			title=getResources().getString(R.string.string_folders) + "\n" + (String) BaseActivity.fileMusicMap.get(indext).get(BaseActivity.Key_Title);
		}
		if (id.equals(BaseActivity.PLAYLIST_MUSIC)) {
			musiclist = (ArrayList<SongObject>) BaseActivity.playListMusicMap.get(indext).get(BaseActivity.Key_List);
			title=getResources().getString(R.string.string_playlist) + "\n" + (String) BaseActivity.playListMusicMap.get(indext).get(BaseActivity.Key_Title);
		}
		for (int i = 0; i < musiclist.size(); i++) {
			Map<String, Object> map = new HashMap<String, Object>();
			map.put(BaseActivity.Key_Title, musiclist.get(i).getTXNAME());
			map.put(BaseActivity.Key_Artist, musiclist.get(i).getNAME_SINGERS());
			map.put(BaseActivity.Key_Music, musiclist.get(i));
			newlist.add(map);
		}
		adapter.setList(newlist, BaseActivity.SONGS_MUSIC);
		listview.setAdapter(adapter);
	}

	@Override
	public void onAttach(Activity activity) {
		super.onAttach(activity);
		if (activity instanceof MainActivity) {
			mActivity = (MainActivity) activity;
		}
	}

}
